//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package ciicos.point.controller;

import ciicos.base.ActionResult;
import ciicos.base.UserInfo;
import ciicos.base.vo.PageListVO;
import ciicos.base.vo.PaginationVO;
import ciicos.exception.DataException;
import ciicos.permission.entity.UserEntity;
import ciicos.point.entity.PointGroupEntity;
import ciicos.point.model.pointgroup.PointGroupForm;
import ciicos.point.model.pointgroup.PointGroupInfoVO;
import ciicos.point.model.pointgroup.PointGroupListVO;
import ciicos.point.model.pointgroup.PointGroupPagination;
import ciicos.point.service.PointGroupService;
import ciicos.util.DateUtil;
import ciicos.util.GeneraterSwapUtil;
import ciicos.util.JsonUtil;
import ciicos.util.RandomUtil;
import ciicos.util.StringUtil;
import ciicos.util.UserProvider;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.List;
import javax.validation.Valid;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Api(
    tags = {"点位分组"},
    value = "pointManage"
)
@RequestMapping({"/api/pointManage/PointGroup"})
public class PointGroupController {
    private static final Logger log = LoggerFactory.getLogger(PointGroupController.class);
    @Autowired
    private GeneraterSwapUtil generaterSwapUtil;
    @Autowired
    private UserProvider userProvider;
    @Autowired
    private PointGroupService pointGroupService;

    @ApiOperation("获取列表")
    @PostMapping({"/getList"})
    public ActionResult list(@RequestBody PointGroupPagination pointGroupPagination) throws IOException {
        List<PointGroupEntity> list = this.pointGroupService.getList(pointGroupPagination);
        List<PointGroupListVO> listVO = JsonUtil.getJsonToList(list, PointGroupListVO.class);

        for(PointGroupListVO pointGroupVO : listVO) {
            pointGroupVO.setOrganizeid_name(this.generaterSwapUtil.comSelectValues(pointGroupVO.getOrganizeid(), false));
            pointGroupVO.setName_name(pointGroupVO.getName());
            pointGroupVO.setDescription_name(pointGroupVO.getDescription());
            pointGroupVO.setCreatoruserid_name(this.generaterSwapUtil.userSelectValue(pointGroupVO.getCreatoruserid()));
            pointGroupVO.setCreatortime_name(pointGroupVO.getCreatortime());
            pointGroupVO.setJsonparam_name(pointGroupVO.getJsonparam());
        }

        PageListVO vo = new PageListVO();
        vo.setList(listVO);
        PaginationVO page = (PaginationVO)JsonUtil.getJsonToBean(pointGroupPagination, PaginationVO.class);
        vo.setPagination(page);
        return ActionResult.success(vo);
    }

    @PostMapping
    @Transactional
    @ApiOperation("创建")
    public ActionResult create(@RequestBody @Valid PointGroupForm pointGroupForm) throws DataException {
        String b = this.pointGroupService.checkForm(pointGroupForm, 0);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            String mainId = RandomUtil.uuId();
            UserInfo userInfo = this.userProvider.get();
            UserEntity userEntity = this.generaterSwapUtil.getUser(userInfo.getUserId());
            pointGroupForm.setCreatoruserid(userInfo.getUserId());
            pointGroupForm.setCreatortime(DateUtil.getNow());
            PointGroupEntity entity = (PointGroupEntity)JsonUtil.getJsonToBean(pointGroupForm, PointGroupEntity.class);
            entity.setId(mainId);
            this.pointGroupService.save(entity);
            return ActionResult.success("创建成功");
        }
    }

    @ApiOperation("信息")
    @GetMapping({"/{id}"})
    public ActionResult<PointGroupInfoVO> info(@PathVariable("id") String id) {
        PointGroupEntity entity = this.pointGroupService.getInfo(id);
        PointGroupInfoVO vo = (PointGroupInfoVO)JsonUtil.getJsonToBean(entity, PointGroupInfoVO.class);
        vo.setCreatoruserid(this.generaterSwapUtil.userSelectValue(vo.getCreatoruserid()));
        if (vo.getCreatortime() != null) {
            vo.setCreatortime(vo.getCreatortime());
        }

        return ActionResult.success(vo);
    }

    @ApiOperation("表单信息(详情页)")
    @GetMapping({"/detail/{id}"})
    public ActionResult<PointGroupInfoVO> detailInfo(@PathVariable("id") String id) {
        PointGroupEntity entity = this.pointGroupService.getInfo(id);
        PointGroupInfoVO vo = (PointGroupInfoVO)JsonUtil.getJsonToBean(entity, PointGroupInfoVO.class);
        vo.setOrganizeid(this.generaterSwapUtil.comSelectValues(vo.getOrganizeid(), false));
        vo.setName(vo.getName());
        vo.setDescription(vo.getDescription());
        vo.setCreatoruserid(this.generaterSwapUtil.userSelectValue(vo.getCreatoruserid()));
        vo.setCreatortime(vo.getCreatortime());
        vo.setJsonparam(vo.getJsonparam());
        return ActionResult.success(vo);
    }

    @PutMapping({"/{id}"})
    @Transactional
    @ApiOperation("更新")
    public ActionResult update(@PathVariable("id") String id, @RequestBody @Valid PointGroupForm pointGroupForm) throws DataException {
        String b = this.pointGroupService.checkForm(pointGroupForm, 1);
        if (StringUtil.isNotEmpty(b)) {
            return ActionResult.fail(b + "不能重复");
        } else {
            UserInfo userInfo = this.userProvider.get();
            PointGroupEntity entity = this.pointGroupService.getInfo(id);
            if (entity != null) {
                PointGroupEntity subentity = (PointGroupEntity)JsonUtil.getJsonToBean(pointGroupForm, PointGroupEntity.class);
                subentity.setCreatoruserid(entity.getCreatoruserid());
                subentity.setCreatortime(entity.getCreatortime());
                boolean b1 = this.pointGroupService.updateById(subentity);
                return !b1 ? ActionResult.fail("当前表单原数据已被调整，请重新进入该页面编辑并提交数据") : ActionResult.success("更新成功");
            } else {
                return ActionResult.fail("更新失败，数据不存在");
            }
        }
    }

    @ApiOperation("删除")
    @DeleteMapping({"/{id}"})
    @Transactional
    public ActionResult delete(@PathVariable("id") String id) {
        PointGroupEntity entity = this.pointGroupService.getInfo(id);
        if (entity != null) {
            this.pointGroupService.delete(entity);
        }

        return ActionResult.success("删除成功");
    }
}
